var express = require('express');
var userdb = require('../model/usersSchema');
var router = express.Router();

/* GET home page. */
// router.get('/', function(req, res, next) {
//   res.render('reg', { userMsg: ''});
// });

router.post('/register',function(req,res,next){
  //接收前端的数据
  res.header("Access-Control-Allow-Origin","*");
  res.header("Access-Control-Allow-Headers","X-Requested-With");
  res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
  var username=req.body.username;
  var password=req.body.password;
  //2、连接数据库，先判断用户名是否存在。不存在就保存;
  userdb.find({"username":username},function(data){
    if(data.length>0){
     res.json({
       "checked":"1",
     })

    }else{
      userdb.add({
        "username":username,
        "password":password
      },function(){
        //3、跳转到“注册成功提示页面”
        // res.redirect("loginCheck");
       res.json({
         "checked":"0"
       })
      });
    }
  });
})
// router.get("/d",function (req,res) {
//     res.send("ssssss");
// })


module.exports =router;
